package com.feiyu.universal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyu.common.controller.BaseController;
import com.feiyu.universal.dto.CommentDTO;
import com.feiyu.universal.entity.Comment;
import com.feiyu.universal.service.CommentService;
import com.feiyu.universal.vo.CommentVO;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/comment")
@AllArgsConstructor
public class CommentController extends BaseController<CommentService,Comment> {

    private CommentService commentService;

    @GetMapping("/detail/{id}")
    public R<Comment> detail(@PathVariable int id){
        return R.data(commentService.getById(id));
    }

    @GetMapping("/page/v1")
    public R<IPage<CommentVO>> page(Page<Comment> page, CommentDTO param){
        return R.data(commentService.pageComment(page,param));
    }

    @PostMapping("/save")
    public R save(@RequestBody Comment comment){
        if(comment==null||comment.getId()==null||comment.getId()==0)
            return R.status(commentService.save(comment));
        return R.status(commentService.updateById(comment));
    }

    /**
     * 更加用户id 查出与用户相关的评论
     * 直接评论资源 博客 回复的评论
     * */
    @GetMapping("/manage/page")
    public R<IPage<CommentVO>> managePage(Page<Comment> page,Long userId,Boolean flag){
        return R.data(commentService.managePage(page,userId,flag));
    }

    @PostMapping("/restore")
    public R restore(Long id){
        return R.status(commentService.restoreComment(id));
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        return R.status(commentService.removeById(id));
    }
}

